﻿using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;

public class Waiting : MonoBehaviour {

    private float fps = 10.0f;
    private float time;
    //一组动画贴图，在编辑器中赋值
    public Texture2D[] animations;

    private int nowFram = 0;
    //是否开始加载，只有等匹配完成后才开始加载
    private bool beginLoad = false;

    //异步对象
    private AsyncOperation async;
    //读取场景对象进度，它的取值范围在0-1之间
    int progress = 0;
    

	// Use this for initialization
	void Start () {
	    
	}
	
	// Update is called once per frame
	void Update () {
	    if(beginLoad)
        {
            progress = (int)(async.progress * 100);
            Debug.Log("loading..." + progress + "%");
        }
	}
    //动态加载场景
    IEnumerator loadScene()
    {
        //异步读取场景
        async = SceneManager.LoadSceneAsync("CompleteMainScene");
        yield return async;
    }
    void OnGUI()
    {
        if(beginLoad)
        {
            DrawAnimation();
        }
    }
    void DrawAnimation()
    {
        time += Time.deltaTime;

        if(time >= 1.0/fps)
        {
            nowFram++;

            time = 0;
            if (nowFram >= animations.Length)
                nowFram = 0;
        }
        GUI.DrawTexture(new Rect(100,100,40,60),animations[nowFram]);

        GUI.Label(new Rect(100,180,300,60),"loading..." + progress + "%");
    }

}
